package ltd.followkba.task;

import com.kcbsdk.MyWorks.APhotoInfo;
import com.kcbsdk.MyWorks.AWorksType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;

import ltd.advskin.MRAM;
import ltd.advskin.utils.SysUtils;
import ltd.followkba.comment.MUTIL;
import ltd.followkba.comment.utils.CodeBase64;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;

public class App39 {

    public static void MLOG(String text) {
        MSTRING("App39 " + text);
    }


    //    flag=1&keywords=快乐解忧&navId=0&pageIndex=1&ispublic=0&sd_version=android|5.5.8&pid=0&self_sign=577e63dfcfccb8d833d3424e414299bb
    public static synchronized String[] searchUserAA(String user) {
        Map<String, String> map = new HashMap<>();
        map.put("flag", "1");
        map.put("keywords", user);//"快乐解忧");
        map.put("sd_version", "android|5.5.8");
        map.put("navId", "0");
        map.put("ispublic", "0");
        map.put("pid", "0");
        map.put("pageIndex", "1");

        map.put("keywords", URLEncoder.encode(user));//"快乐解忧"));
        map.put("self_sign", getSign(map));
        String destUrl = "https://ks.weinisongdu.com/api/search";
        String result = httpsPost(destUrl, getData(map));
        SysUtils sysUtils = new SysUtils();
        if (result != null) {
            try {
                JSONArray jsonArray = new JSONArray(result);
                int length = jsonArray.length();
                String[] userIdS = new String[length];
                for (int index = 0; index < length; index++) {
                    JSONObject jsonobject = jsonArray.getJSONObject(index);
                    if (jsonobject != null) {
                        userIdS[index] = sysUtils.getJsonString(jsonobject.toString(), "id");
                    }
                }
                return userIdS;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static synchronized String[] searchUser(String user) {
        String destUrl = "https://ks.weinisongdu.com/api/search";
        Map<String, String> map = new HashMap<>();
        map.put("flag", "1");
        map.put("keywords", user);//"快乐解忧");
        map.put("ks_version", "android|2.5.0");
        map.put("pageIndex", "1");
        map.put("count", "20");
        String sign = getSign(map);

        map.put("keywords", URLEncoder.encode(user));//"快乐解忧"));
        map.put("self_sign", sign);
        String data = getData(map);
        String result = httpsPost(destUrl, data);
//        MLOG("bAppUser.WorksS[index].search "+result);
        SysUtils sysUtils = new SysUtils();
        if (result != null) {
            try {
                JSONArray jsonArray = new JSONArray(result);
                int length = jsonArray.length();
                String[] userIdS = new String[length];
                for (int index = 0; index < length; index++) {
                    JSONObject jsonobject = jsonArray.getJSONObject(index);
                    if (jsonobject != null) {
                        userIdS[index] = sysUtils.getJsonString(jsonobject.toString(), "id");
                    }
                }
                return userIdS;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //    https://ks.weinisongdu.com/api/accorOpusIdGetInfo
//    opusId=1388881&ks_version=android|2.5.0&self_sign=fb906f3e414ccd9b65de7aef91ff4874
    public static synchronized BAppWorks getWorksInfo(String worksS) {
        Map<String, String> map = new HashMap<>();
        map.put("opusId", worksS);
        String result = noEncodePost(map, "https://ks.weinisongdu.com/api/accorOpusIdGetInfo");
//        MLOG(String.format("HomeA bAppUser %s %s", worksS, result));
        if (result != null){
            SysUtils sysUtils = new SysUtils();
            BAppWorks bAppWorks = new BAppWorks();
            bAppWorks.WorksType = 39;
            bAppWorks.UserIdS = sysUtils.getJsonString(result, "uid");
            bAppWorks.WorksIdS = sysUtils.getJsonString(result, "id");
            bAppWorks.WorksName = sysUtils.getJsonString(result, "name");
            bAppWorks.ArtistName = sysUtils.getJsonString(result, "writername");
            bAppWorks.MusicIdS = sysUtils.getJsonString(result, "poemid");
            bAppWorks.Intro = sysUtils.getJsonString(result, "content");
            bAppWorks.AudioUrl = sysUtils.getJsonString(result, "url");
            bAppWorks.CommentQty = sysUtils.getJsonInt(result, "commentnum");;
            bAppWorks.ListenQty = sysUtils.getJsonInt(result, "lnum");
            bAppWorks.GiftQty = sysUtils.getJsonInt(result, "flower_num");
            bAppWorks.ShareQty = sysUtils.getJsonInt(result, "sharenum");
            bAppWorks.MoneyQty = sysUtils.getJsonInt(result, "giftnum");
            bAppWorks.FreeQty = sysUtils.getJsonInt(result, "flower_num");
            bAppWorks.LikeQty = sysUtils.getJsonInt(result, "praisenum");
            bAppWorks.CreateTime = sysUtils.getJsonString(result, "addtime");
            bAppWorks.LyricUrl = sysUtils.getJsonString(result, "lyricurl");
            MLOG(String.format("HomeA bAppUser %s(%s)%s", bAppWorks.WorksName, bAppWorks.ArtistName, bAppWorks.Intro));
            return bAppWorks;
        }
        return null;
    }

    //    https://www.weinisongdu.com/api/getOpusList
//    add=0&otherId=20234508&pageIndex=1&sd_version=android|5.5.2&count=20&self_sign=dbd593f5758ab9d8142a96d6a0c400a4

//    https://ks.weinisongdu.com/api/getOpusList
//    otherId=24691141&ks_version=android|2.5.0&pageIndex=3&count=20&self_sign=dcd42b59e25a392303468c65db29f760

    //        String destUrl = "https://ks.weinisongdu.com/api/accorOpusIdGetInfo";
//        String text = String.format("opusId=%s&ks_version=android|2.5.0&self_sign=399f787097552b09b9484b8a54d182a1", userIdS);
//    String destUrl = "https://www.weinisongdu.com/api/getOpusList";
//    String text = String.format("add=0&otherId=%s&pageIndex=1&sd_version=android|5.5.8&count=20&self_sign=%s", userIdS, "684961c5980addb9e3be8549cf2be6b8");
//  return httpsPost(destUrl, text);


//    https://ks.weinisongdu.com/api/getOpusList
//    https://ks.weinisongdu.com/api/getOpusList
//    otherId=24691141&ks_version=android|2.5.0&pageIndex=2&count=20&self_sign=155c9e73d2a02e4ef3cfa730f45a6905
    public static synchronized String[] getWorksList(String userIdS, boolean getAll){
        if (false){
            int length = getAll ? 1000 : 1;
            List<String> lists = new ArrayList<>();
            for (int index = 0; index < length; index++) {
                Map<String, String> map = new HashMap<>();
                map.put("otherId", userIdS);
                map.put("pageIndex", String.format("%s", index + 2));
                map.put("count", "20");

                String result = noEncodePost(map, "https://ks.weinisongdu.com/api/getOpusList");

//            String result = httpsPost("https://ks.weinisongdu.com/api/getOpusList", "otherId=24691141&ks_version=android|2.5.0&pageIndex=2&count=20&self_sign=155c9e73d2a02e4ef3cfa730f45a6905");

                MLOG(" HomeA resultA "+result);
            }
            return lists.toArray(new String[lists.size()]);

        }else {
            Map<String, String> map = new HashMap<>();
            map.put("otherId", userIdS);
            String result = noEncodePost(map, "https://ks.weinisongdu.com/api/userCenter");
            if (result != null) {
                SysUtils sysUtils = new SysUtils();
                String text = sysUtils.getJsonString(result, "userInfo");
                try {
                    JSONArray jsonArray = new JSONArray(sysUtils.getJsonString(text, "opuslist"));
                    int length = jsonArray.length();
                    String[] worksIdS = new String[length];
                    for (int index = 0; index < length; index++) {
                        JSONObject jsonobject = jsonArray.getJSONObject(index);
                        if (jsonobject != null) {
                            text = jsonobject.toString();
                            worksIdS[index] = sysUtils.getJsonString(text, "id");
                        }
                    }
                    return worksIdS;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    public static synchronized BAppUser getUserInfo(String userIdS) {
        Map<String, String> map = new HashMap<>();
        map.put("otherId", userIdS);
        String result = noEncodePost(map, "https://ks.weinisongdu.com/api/userCenter");
//        map.put("ks_version", "android|2.5.0");         // 2.5.0
////        String text = "otherId=" + userIdS + "&ks_version=android|2.5.0&self_sign=30909ff361b130b5e7c845498842c157";
////        text = "otherId=24622499&ks_version=android|2.5.0&self_sign=ec022fc646a4afbf8a82972b1ad158f0";
//        map.put("self_sign", getSign(map));
//        String destUrl = readpoem ? "https://www.weinisongdu.com/api/userCenter" : "https://ks.weinisongdu.com/api/userCenter";
//        String result = httpsPost(destUrl, getData(map));
        if (result != null) {
            SysUtils sysUtils = new SysUtils();
            BAppUser bAppUser = new BAppUser();
//            MLOG(String.format("HomeA result E %s", result));
            String text = sysUtils.getJsonString(result, "userInfo");
            bAppUser.WorksType = 39;
            bAppUser.UserIdS = sysUtils.getJsonString(text, "id");
            bAppUser.UserName = sysUtils.getJsonString(text, "nick");
            bAppUser.Gender = sysUtils.getJsonInt(text, "gender");
            bAppUser.HeadUrl = sysUtils.getJsonString(text, "portrait");    // portrait为大图，sportrait为小图
            bAppUser.HeadFile = String.format("%sU39_%s.jpg", MRAM.gsCachePath, bAppUser.UserIdS);
//            MLOG(String.format("HomeA Gender %s(%s)%s", bAppUser.Gender, sysUtils.getJsonString(text, "sportrait"), sysUtils.getJsonString(text, "portrait")));
            bAppUser.Age = sysUtils.getJsonInt(text, "age");

            bAppUser.Intro = sysUtils.getJsonString(text, "signature");
            bAppUser.WorksQty = sysUtils.getJsonInt(text, "opusnum");
            bAppUser.FansQty = sysUtils.getJsonInt(text, "fans");
            bAppUser.CareQty = sysUtils.getJsonInt(text, "attention");
            bAppUser.LikeQty = sysUtils.getJsonInt(text, "praisenum");
            String user_address = sysUtils.getJsonString(text, "user_address");
            if (user_address != null) {
                String[] split = user_address.split(" ");
                if (split.length > 1) {
                    bAppUser.Province = split[1];
                    if (split.length > 2) {
                        bAppUser.City = split[2];
                    }
                }
            }

            try {
                JSONArray jsonArray = new JSONArray(sysUtils.getJsonString(text, "bgpic"));
                int length = jsonArray.length();
                MLOG(String.format("HomeA bgpic E %s", length));
                bAppUser.PhotoS = new APhotoInfo[length];
                for (int index = 0; index < length; index++) {
                    JSONObject jsonobject = jsonArray.getJSONObject(index);
                    if (jsonobject != null) {
                        bAppUser.PhotoS[index] = new APhotoInfo();
                        bAppUser.PhotoS[index].PhotoID = sysUtils.getJsonString(jsonobject.toString(), "id");
                        bAppUser.PhotoS[index].FileUrl = sysUtils.getJsonString(jsonobject.toString(), "url");
                        bAppUser.PhotoS[index].FileName = String.format("%sP39_%s.jpg", MRAM.gsCachePath, bAppUser.PhotoS[index].PhotoID);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
//
//            try {
//                JSONArray jsonArray = new JSONArray(sysUtils.getJsonString(text, "opuslist"));
//                int length = jsonArray.length();
//                bAppUser.WorksS = new BAppWorks[length];
//                for (int index = 0; index < length; index++) {
//                    JSONObject jsonobject = jsonArray.getJSONObject(index);
//                    if (jsonobject != null) {
//                        text = jsonobject.toString();
//                        BAppWorks bAppWorks = new BAppWorks();
//                        bAppUser.WorksS[index] = bAppWorks;
//                        bAppWorks.WorksType = 39;
//                        bAppWorks.UserIdS = bAppUser.UserIdS;
//                        bAppWorks.IdS = sysUtils.getJsonString(text, "id");
//
//                        bAppWorks.WorksName = sysUtils.getJsonString(text, "name");
//                        bAppWorks.ArtistName = sysUtils.getJsonString(text, "writername");
//                        bAppWorks.TimeLength = sysUtils.getJsonInt(text, "opustime") * 1000;
//
//                        bAppWorks.Intro = sysUtils.getJsonString(text, "content");
//                        bAppWorks.LyricUrl = sysUtils.getJsonString(text, "lyricurl");
//                        bAppWorks.AudioUrl = sysUtils.getJsonString(text, "url");
//                        bAppWorks.MusicIdS = sysUtils.getJsonString(text, "poemid");
//                        bAppWorks.CoverUrl = sysUtils.getJsonString(text, "portrait");
//                        bAppWorks.CoverFile = String.format("%sC28_%s.jpg", MRAM.gsCachePath, bAppWorks.IdS);
//
//                        bAppWorks.CommentQty = sysUtils.getJsonInt(text, "lnum");
//                        bAppWorks.CommentQty = sysUtils.getJsonInt(text, "repostnum");
//                        bAppWorks.CommentQty = sysUtils.getJsonInt(text, "praisenum");
//                        bAppWorks.CommentQty = sysUtils.getJsonInt(text, "downnum");
//                        bAppWorks.CommentQty = sysUtils.getJsonInt(text, "collectnum");
//                        bAppWorks.GiftQty = sysUtils.getJsonInt(text, "giftnum");
//
//                        bAppWorks.CommentQty = sysUtils.getJsonInt(text, "commentnum");
//                        long addtime = sysUtils.getJsonInt(text, "addtime");
////                        addtime *= 1000;
////                        long tick = System.currentTimeMillis();
//                        bAppWorks.CreateTime = String.format("%s", addtime);//MUTIL.CHANG_DATE_FORMAT(String.format("%s", addtime));
//
////                        String svrUrl = "http://poem.weinisongdu.com/upload/";
////                        svrUrl = String.format("%slyric/default/b%07d.lrc", svrUrl, poemid);
//                        MLOG(String.format("bAppWorks.WorksS %s %s %s", bAppWorks.WorksName, bAppWorks.MusicIdS, bAppWorks.LyricUrl));
//
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            return bAppUser;
        }
//        String destUrl = "https://www.weinisongdu.com/api/userCenter";
//        String text = String.format("otherId=%s&pageIndex=0&sd_version=android|5.5.8&count=0&self_sign=%s", userIdS, LoginFlag);
//        otherId=20234508&ks_version=android|2.5.0&self_sign=30909ff361b130b5e7c845498842c157

//        MLOG("resultA " + result);
//        SysUtils sysUtils = new SysUtils();
//        String message = sysUtils.getJsonString(result, "message");
//        if ("success".equals(message)) {
//            text = sysUtils.getJsonString(result, "data");
//            try {
//                MLOG("resultA1 " + text);
//                byte[] str = CodeBase64.decode(text.getBytes());
//                MLOG("resultA2 " + str);
//                return "ok";
////            text
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

        return null;
    }


    public static synchronized BAppWorks getPoemInfo(String musicIdS){
        Map<String, String> map = new HashMap<>();
        map.put("poemId", musicIdS);
        String result = noEncodePost(map, "https://ks.weinisongdu.com/api/accorPoemGetInfo");
        if (result != null){
            SysUtils sysUtils = new SysUtils();
            BAppWorks bAppWorks = new BAppWorks();
            bAppWorks.MusicUrl = sysUtils.getJsonString(result, "burl");
            bAppWorks.OriginalUrl = sysUtils.getJsonString(result, "yurl");
            bAppWorks.LyricUrl = sysUtils.getJsonString(result, "lyricurl");
            return bAppWorks;
        }
        return null;
    }


    public static synchronized String getCareList(String UserIdS, int index) {
//        flag=0&otherId=24381812&ks_version=android|2.4.9&pageIndex=1&count=20&self_sign=b3d2f5c8d5f19cd257f08a864c571849
        Map<String, String> map = new HashMap<>();
        map.put("flag", "0");
        map.put("otherId", UserIdS);
        map.put("pageIndex", String.format("%s", index+1));
        map.put("count", "20");
        String result = noEncodePost(map, "https://ks.weinisongdu.com/api/followList");
        if (result != null){
//            SysUtils sysUtils = new SysUtils();
//            BAppWorks bAppWorks = new BAppWorks();
//            bAppWorks.MusicUrl = sysUtils.getJsonString(result, "burl");
//            bAppWorks.OriginalUrl = sysUtils.getJsonString(result, "yurl");
//            bAppWorks.LyricUrl = sysUtils.getJsonString(result, "lyricurl");
//            return bAppWorks;
        }
        return null;
//
//
    }

    private static synchronized String noEncodePost(Map<String, String> map, String destUrl){
        map.put("ks_version", "android|2.5.0");         // 2.5.0
        map.put("self_sign", getSign(map));
        return httpsPost(destUrl, getData(map));
    }

    /*
https://ks.weinisongdu.com/api/follow
fid=27073871&ks_version=android|2.5.0&self_sign=bd33368cea90b1f7a92587d3a94f4c6a
https://ks.weinisongdu.com/api/userCenter
otherId=27073871&ks_version=android|2.5.0&self_sign=6e23c126de791a73f3328bdbfb1f9f02
https://ks.weinisongdu.com/api/accorOpusIdGetInfo
opusId=1400577&ks_version=android|2.5.0&self_sign=621cc747b8ea71d60bf8e7ae5ca352a4
https://ks.weinisongdu.com/api/commentList
opusId=1400577&flag=1&ks_version=android|2.5.0&pageIndex=1&count=20&self_sign=071b09191293a4a1811ddaff01655dc4
https://ks.weinisongdu.com/api/poemDetailBillbord
cate=0&flag=1&ks_version=android|2.5.0&pageIndex=1&count=20&poemId=92&status=0&self_sign=f9962cbb71e28be54303407b589cede0
https://ks.weinisongdu.com/api/opusListen
opusId=1400577&ks_version=android|2.5.0&self_sign=621cc747b8ea71d60bf8e7ae5ca352a4
http://kpoem.weinisongdu.com/upload/poem/1615132800/161521904060464960a249f.mp3
http://kpoem.weinisongdu.com/upload/lyric/default/b10000014.lrc
http://kpoem.weinisongdu.com/upload/poem/1615132800/161521904060464960a249f.mp3

https://ks.weinisongdu.com/api/accorPoemGetInfo
ks_version=android|2.5.0&poemId=92&self_sign=554524cf3f3cba4055c8b8e39d530a15


03-09 18:42:00.029 10733-10834/ltd.followkba W/MSYS: LoginTaskPage->onInitData() bAppUser.WorksS[index].userIdS[0] 27073871
03-10 09:01:01.474 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 民歌 null 1515 http://kpoem.weinisongdu.com/upload/lyric/default/b0001291.lrc
03-10 09:01:01.475 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 民歌 null 93 http://kpoem.weinisongdu.com/upload/lyric/default/b0000013.lrc
03-10 09:01:01.476 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 茅屋为秋风所破歌 null 92 http://kpoem.weinisongdu.com/upload/lyric/default/b10000014.lrc
03-10 09:01:01.477 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 深夜里听到乐声 null 95 http://kpoem.weinisongdu.com/upload/lyric/default/b0000011.lrc
03-10 09:01:01.478 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 陋室铭 null 101 http://kpoem.weinisongdu.com/upload/lyric/default/b0000005.lrc
03-10 09:01:01.479 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 当你老了 null 102 http://kpoem.weinisongdu.com/upload/lyric/default/b0000004.lrc
03-10 09:01:01.480 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 2021年新年贺词 null 6129 http://kpoem.weinisongdu.com/upload/lyric/default/7e69192958dc5c398ad2a75b7c96a9be.lrc
03-10 09:01:01.481 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 蝶恋妃 null 104 http://kpoem.weinisongdu.com/upload/lyric/default/b0000002.lrc
03-10 09:01:01.482 1884-2026/ltd.followkba W/MSYS: App39->getUserInfo() bAppWorks.WorksS 习主席寄语 null 1269 http://kpoem.weinisongdu.com/upload/lyric/default/b0000993.lrc
当你老了                04      102     b0000004.lrc
茅屋为秋风所破歌        0e      92      b10000014.lrc
民歌 93.lrc 0d

http://kpoem.weinisongdu.com/upload/lyric/default/b10000014.lrc



https://ks.weinisongdu.com/api/search
flag=3&keywords=茅屋为秋风所破歌&ks_version=android|2.5.0&pageIndex=1&count=20&self_sign=a7cf18e857bbe236bb54b5f961b2fc27
https://ks.weinisongdu.com/api/downloadPoem
ks_version=android|2.5.0&poemId=92&self_sign=554524cf3f3cba4055c8b8e39d530a15
https://ks.weinisongdu.com/api/accorPoemGetInfo
ks_version=android|2.5.0&poemId=92&self_sign=554524cf3f3cba4055c8b8e39d530a15

     */

















    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";
    private static final String KEY_ALGORITHM = "AES";
    public static final int SECRET_KEY_LENGTH = 16;
    private static final String THE_OFFSET = "9F8275438BEAC19A";

    public static synchronized String decrypt(String text) {
        try {
            byte[] data = CodeBase64.decode(text.getBytes());
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(2, new SecretKeySpec("E9809FF9732A1F97".getBytes("utf-8"), KEY_ALGORITHM), new IvParameterSpec(THE_OFFSET.getBytes()));
            String result = new String(cipher.doFinal(data), "utf-8");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static synchronized String getSign(String text){
        text = "quanminKshi" + text + "xvBG&VCmMagSufzRjydqBMxyU3dA^P#1";
        SysUtils sysUtils = new SysUtils();
        return sysUtils.getStringMd5(text);
    }
    private static final String SIGN_HEADER = "quanminKshi";
    private static final String SIGN_NAME = "self_sign";
    private static final String SIGN_TAILER = "xvBG&VCmMagSufzRjydqBMxyU3dA^P#1";

    public static synchronized String getData(Map<String, String> map){
        List<String> keys = new ArrayList<>();
        if (map != null && map.size() > 0) {
            for (String s : map.keySet()) {
                keys.add(s);
            }
        }
        Collections.sort(keys, new Comparator<String>() {
            public int compare(String s, String t1) {
                return s.compareTo(t1);
            }
        });
        StringBuilder stringBuilder = new StringBuilder();
        if (map != null && map.size() > 0) {
            for (String item : keys) {
                stringBuilder.append("&");
                stringBuilder.append(item);
                stringBuilder.append("=");
                stringBuilder.append(map.get(item));
            }
        }
        return stringBuilder.toString();
    }
    public static synchronized String getSign(Map<String, String> map){
        List<String> keys = new ArrayList<>();
        if (map != null && map.size() > 0) {
            for (String s : map.keySet()) {
                keys.add(s);
            }
        }
        Collections.sort(keys, new Comparator<String>() {
            public int compare(String s, String t1) {
                return s.compareTo(t1);
            }
        });
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(SIGN_HEADER);
        if (map != null && map.size() > 0) {
            for (String item : keys) {
                if (!SIGN_NAME.equals(item)) {
                    stringBuilder.append(map.get(item));
                }
            }
        }
        stringBuilder.append(SIGN_TAILER);
        String text = stringBuilder.toString();
//        MLOG("WorksS "+text);
        SysUtils sysUtils = new SysUtils();
        String md5 = sysUtils.getStringMd5(text);
        return md5;
//        return Md5Util.getMd5(stringBuilder.toString());


    }
    private static synchronized String httpsPost(String destUrl, String text){
        try {
            URL url = new URL(destUrl);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setDoInput(true);
            if (text == null) {
                connection.setRequestMethod("GET");
            }else {
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                out.writeBytes(text);
                out.flush();
                out.close();
            }
            connection.connect();
            StringBuilder builder = new StringBuilder();
            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
            while((line = reader.readLine())!=null){
                builder.append(line);
            }
            reader.close();
            connection.disconnect();
            String result = builder.toString();
            SysUtils sysUtils = new SysUtils();
            String message = sysUtils.getJsonString(result, "message");
            if ("success".equals(message)) {
                return decrypt(sysUtils.getJsonString(result, "data"));
            }
            return message;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//
//
//    private static TrustManager myX509TrustManager = new X509TrustManager() {
//
//        @Override
//        public X509Certificate[] getAcceptedIssuers() {
//            return null;
//        }
//
//        @Override
//        public void checkServerTrusted(X509Certificate[] chain, String authType)
//                throws CertificateException {
//        }
//
//        @Override
//        public void checkClientTrusted(X509Certificate[] chain, String authType)
//                throws CertificateException {
//        }
//    };
//
//    static public String SendHttpsPOST(String url, String data) {
//        String result = null;
//        //使用此工具可以将键值对编码成"Key=Value&amp;Key2=Value2&amp;Key3=Value3&rdquo;形式的请求参数
////        String requestParam = URLEncodedUtils.format(param, "UTF-8");
//        try {
//            //设置SSLContext
//            SSLContext sslcontext = SSLContext.getInstance("TLS");
//            sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
//
//            //打开连接
//            //要发送的POST请求url?Key=Value&amp;Key2=Value2&amp;Key3=Value3的形式
//            URL requestUrl = new URL(url);
//            HttpsURLConnection httpsConn = (HttpsURLConnection) requestUrl.openConnection();
//
//            //设置套接工厂
//            httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory());
//
//            //加入数据
//            httpsConn.setRequestMethod("POST");
//            httpsConn.setDoOutput(true);
//            DataOutputStream out = new DataOutputStream(
//                    httpsConn.getOutputStream());
//            if (data != null)
//                out.writeBytes(data);
//
//            out.flush();
//            out.close();
//
//            //获取输入流
//            BufferedReader in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream()));
//            int code = httpsConn.getResponseCode();
//            if (HttpsURLConnection.HTTP_OK == code) {
//                String temp = in.readLine();
//                /*连接成一个字符串*/
//                while (temp != null) {
//                    if (result != null)
//                        result += temp;
//                    else
//                        result = temp;
//                    temp = in.readLine();
//                }
//            }
//        } catch (KeyManagementException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (ProtocolException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }
}